package Railroad.Components;

// Trieda definuje všetky vlsatnosti výhybky.

public class Switch extends TrackNode {
    private final int startX;
    private final int endX;
    private final int directY;
    private final int sideY;
    private final int sraightStart;
    private final int sraightEnd;
    private final String name;
    private final int maxRychlost;
    private boolean hasStatusRequest;  // vyhybka ma byt prestavena do inej polohy
    private SwitchStatus requestedStatus;
    
    private int idInDevice;     // adresa vyhybky v zariadeni
    private SwitchStatus status = SwitchStatus.STRAIGHT;
   
    public Switch(int id, int startX, int endX, int directY, int sideY, int sraightStart, int sraightEnd, String name,int rychlost) {
        super(id);
        this.startX = startX;
        this.endX = endX;
        this.directY = directY;
        this.sideY = sideY;
        this.sraightStart = sraightStart;
        this.sraightEnd = sraightEnd;
        this.name = name;
        this.maxRychlost=rychlost;
    }
      
    public SwitchStatus getStatus() {
        return this.status;
    }
    
    public boolean isStraightDirection() {
        return status == SwitchStatus.STRAIGHT;
    }
    
    // Metóda, ktorá prestaví výhybku.
    public void prestav(SwitchStatus smer) {      
        this.status = smer;   
        if (smer == requestedStatus)
            this.hasStatusRequest = false;
    }
    
    // nastavi ziadost na prestavenie
    public void ziadostNaPrestavenie(SwitchStatus smer) {
        hasStatusRequest = true;
        requestedStatus = smer;
    }
    
    public void setIdInDevice(int idInDevice) {
        this.idInDevice = idInDevice;
    }
    
    // Gettre.
    public int getDirectY() {
        return directY;
    }

    public int getEndX() {
        return endX;
    }

    public String getName() {
        return name;
    }

    public int getSideY() {
        return sideY;
    }

    public int getSraightEnd() {
        return sraightEnd;
    }

    public int getSraightStart() {
        return sraightStart;
    }

    public int getStartX() {
        return startX;
    }
    
    public boolean hasStatusRequest() {
        return hasStatusRequest;
    }
    
    public void clearStatusRequest() {
        hasStatusRequest = false;
    }
    
    public SwitchStatus getRequestedStatus() {
        return requestedStatus;
    }
    
    public int getIdInDevice() {
        return idInDevice;
    }

    public int getMaxRychlost() {
        return maxRychlost;
    }
    
    @Override
    public boolean isOnXY(int x, int y) {
        return x >= Math.min(startX, endX) && x <= Math.max(startX, endX) &&
                y >= Math.min(directY, sideY) && y <= Math.max(directY, sideY);
    }
}